{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 72,
   "metadata": {},
   "outputs": [],
   "source": [
    "import yaml\n",
    "with open(\"项目基本信息.yaml\", \"r\", encoding=\"utf-8\") as f:\n",
    "    config = yaml.safe_load(f)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "metadata": {},
   "outputs": [],
   "source": [
    "project_list = config[\"project_list\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{'施工单位': '浙江交工宏途交通建设有限公司',\n",
       "  '监理单位': '浙江浙中建设工程管理有限公司',\n",
       "  '施工部位': 'K59+639圆管涵',\n",
       "  '工程地点及桩号': 'K59+639圆管涵',\n",
       "  '具体部位': '管节安装及基础',\n",
       "  '施工日期': '2024/05/31',\n",
       "  '基础宽度': 2759,\n",
       "  '基础厚度': 300,\n",
       "  '设计坐标(X,Y)': [3243273.926, 509154.842],\n",
       "  '基础底高程': 73.56,\n",
       "  '基础顶高程': 74.06,\n",
       "  '圆管涵顶面高程': 76.16,\n",
       "  '设计高程': 66,\n",
       "  '断面尺寸宽度(直径)': 1250,\n",
       "  '断面尺寸宽度': 275.9,\n",
       "  '断面尺寸高（厚）度': 30,\n",
       "  'instance_mapping': {'JS101': [{'基础底': {'施工部位': 'K59+639圆管涵 基础底',\n",
       "      '长': 44.5,\n",
       "      '宽': 4}},\n",
       "    {'基础顶': {'施工部位': 'K59+639圆管涵 基础顶', '长': 44.5, '宽': 4}},\n",
       "    {'顶面': {'施工部位': 'K59+639圆管涵 顶面', '长': 44.5, '宽': 4}}],\n",
       "   'JS601_1': [{'基础': {'混凝土强度（砂浆）等级': 'C25',\n",
       "      '设计混凝土（砂浆）用量': '15',\n",
       "      '实际混凝土（砂浆）用量': '16'}},\n",
       "    {'护壁': {'混凝土强度（砂浆）等级': 'C30', '设计混凝土（砂浆）用量': '18', '实际混凝土（砂浆）用量': '17'}}],\n",
       "   'JS107': [{'基础': {'基础宽度': 2759,\n",
       "      '基础厚度': 300,\n",
       "      '基础顶高程': 66035,\n",
       "      'JS107规则类型': '1'}},\n",
       "    {'混凝土涵管安装': {'基础宽度': 2759,\n",
       "      '基础厚度': 300,\n",
       "      '基础底高程': 73.56,\n",
       "      '基础顶高程': 74.06,\n",
       "      'JS107规则类型': '2'}}]}}]"
      ]
     },
     "execution_count": 80,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "project_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 81,
   "metadata": {},
   "outputs": [],
   "source": [
    "def process_project_data(project):\n",
    "    # 获取公共字段\n",
    "    common_fields = {k: v for k, v in project.items() if k != 'instance_mapping'}\n",
    "\n",
    "    # 处理每个实例\n",
    "    instances = {}\n",
    "    for instance_name, modules in project['instance_mapping'].items():\n",
    "        processed_modules = []\n",
    "        for module in modules:\n",
    "            # 继承公共字段，并用模块内字段覆盖\n",
    "            merged = common_fields.copy()\n",
    "            merged.update(module)\n",
    "            processed_modules.append(merged)\n",
    "        instances[instance_name] = processed_modules\n",
    "\n",
    "    return instances\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "metadata": {},
   "outputs": [
    {
     "ename": "AttributeError",
     "evalue": "'list' object has no attribute 'items'",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mAttributeError\u001b[0m                            Traceback (most recent call last)",
      "Cell \u001b[0;32mIn[82], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m reslut\u001b[38;5;241m=\u001b[39m\u001b[43mprocess_project_data\u001b[49m\u001b[43m(\u001b[49m\u001b[43mproject_list\u001b[49m\u001b[43m)\u001b[49m\n",
      "Cell \u001b[0;32mIn[81], line 3\u001b[0m, in \u001b[0;36mprocess_project_data\u001b[0;34m(project)\u001b[0m\n\u001b[1;32m      1\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mprocess_project_data\u001b[39m(project):\n\u001b[1;32m      2\u001b[0m     \u001b[38;5;66;03m# 获取公共字段\u001b[39;00m\n\u001b[0;32m----> 3\u001b[0m     common_fields \u001b[38;5;241m=\u001b[39m {k: v \u001b[38;5;28;01mfor\u001b[39;00m k, v \u001b[38;5;129;01min\u001b[39;00m \u001b[43mproject\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mitems\u001b[49m() \u001b[38;5;28;01mif\u001b[39;00m k \u001b[38;5;241m!=\u001b[39m \u001b[38;5;124m'\u001b[39m\u001b[38;5;124minstance_mapping\u001b[39m\u001b[38;5;124m'\u001b[39m}\n\u001b[1;32m      5\u001b[0m     \u001b[38;5;66;03m# 处理每个实例\u001b[39;00m\n\u001b[1;32m      6\u001b[0m     instances \u001b[38;5;241m=\u001b[39m {}\n",
      "\u001b[0;31mAttributeError\u001b[0m: 'list' object has no attribute 'items'"
     ]
    }
   ],
   "source": [
    "reslut=process_project_data(project_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "metadata": {},
   "outputs": [],
   "source": [
    "RULE_MODULES = [\n",
    "    {\"rule_module\": \"GL105\", \"rule_class\": \"GL105_Rule\"},\n",
    "    {\"rule_module\": \"JS101\", \"rule_class\": \"JS101Rule\" },\n",
    "    {\"rule_module\": \"JS103\", \"rule_class\": \"JS103_Rule\"},\n",
    "    {\"rule_module\": \"JS107\", \"rule_class\": \"JS107_Rule\"},\n",
    "    {\"rule_module\": \"JS108\", \"rule_class\": \"JS108_Rule\"},\n",
    "    {\"rule_module\": \"JS111\", \"rule_class\": \"JS111_Rule\"},\n",
    "    {\"rule_module\": \"JS601_1\", \"rule_class\": \"JS601_1_Rule\"},\n",
    "    {\"rule_module\": \"JS703\", \"rule_class\": \"JS703_Rule\"},\n",
    "    {\"rule_module\": \"ZJ403\", \"rule_class\": \"ZJ403_Rule\"},\n",
    "    {\"rule_module\": \"ZJ904\", \"rule_class\": \"ZJ904_Rule\"},\n",
    "]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "metadata": {},
   "outputs": [],
   "source": [
    "def prepare_rule_inputs(project_list, rule_modules=RULE_MODULES):\n",
    "    \"\"\"\n",
    "    将 project_list 预处理成规则执行所需的输入列表\n",
    "\n",
    "    :param project_list: 原始项目配置列表\n",
    "    :param rule_modules: 规则模块定义列表\n",
    "    :return: 规则执行所需的输入列表，每个元素包含：\n",
    "             - rule_module\n",
    "             - rule_class\n",
    "             - project_info\n",
    "             - instance_id\n",
    "             - group_key\n",
    "    \"\"\"\n",
    "    prepared_inputs = []\n",
    "\n",
    "    for project_info in project_list:\n",
    "        instance_mapping = project_info.get(\"instance_mapping\", {})\n",
    "\n",
    "        for rule_config in rule_modules:\n",
    "            module_name = rule_config[\"rule_module\"]\n",
    "            rule_class = rule_config[\"rule_class\"]\n",
    "\n",
    "            if module_name in instance_mapping:\n",
    "                for instance_dict in instance_mapping[module_name]:\n",
    "                    for instance_id, instance_values in instance_dict.items():\n",
    "                        print(f\"Preparing input for {module_name} instance {instance_id}\")\n",
    "\n",
    "                        # 合并字段\n",
    "                        override_fields = {\"桩号、部位\", \"长\", \"宽\"}\n",
    "                        merged_project_info = {\n",
    "                        \n",
    "                            **project_info,\n",
    "                            **{k: v for k, v in instance_values.items() if k in override_fields},\n",
    "                            'instance_id': instance_id,\n",
    "                        }\n",
    "\n",
    "                        prepared_inputs.append({\n",
    "                            \"rule_module\": module_name,\n",
    "                            \"rule_class\": rule_class,\n",
    "                            \"project_info\": merged_project_info,\n",
    "                            \"instance_id\": instance_id,\n",
    "                            \"group_key\": f\"{module_name}_{instance_id}\"\n",
    "                        })\n",
    "            else:\n",
    "                # 没有实例映射时，使用全局 project_info\n",
    "                merged_project_info = {\n",
    "                \n",
    "                    **project_info\n",
    "                }\n",
    "\n",
    "                prepared_inputs.append({\n",
    "                    \"rule_module\": module_name,\n",
    "                    \"rule_class\": rule_class,\n",
    "                    \"project_info\": merged_project_info,\n",
    "                    \"instance_id\": None,\n",
    "                    \"group_key\": f\"{module_name}_default\"\n",
    "                })\n",
    "\n",
    "    return prepared_inputs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Preparing input for JS101 instance 基础底\n",
      "Preparing input for JS101 instance 基础顶\n",
      "Preparing input for JS101 instance 顶面\n",
      "Preparing input for JS107 instance 基础\n",
      "Preparing input for JS107 instance 混凝土涵管安装\n",
      "Preparing input for JS601_1 instance 基础\n",
      "Preparing input for JS601_1 instance 护壁\n"
     ]
    }
   ],
   "source": [
    "prepared_inputs=prepare_rule_inputs(project_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.16"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
